
BUILTINS := $(wildcard src/interpreter/builtins/*/*)
REDUCE_FILES := $(wildcard src/reduce/reduce_files/*)
EXAMPLES := $(wildcard src/web/examples/*.lsp)
VERSION := $(shell cat VERSION)

.PHONY: all web_all node_all \
				clean web_clean node_clean \
				test web_test node_test \
				web_reduce node_reduce \
				web_core node_core web_terminal web_examples dist

all: web_all node_all

web_all: web_core web_examples web_terminal web_test

node_all: node_core node_test

clean:
	-rm -rf build/*

web_clean:
	-rm -rf build/web/*

node_clean:
	-rm -rf build/node/*

web_core: build/web/js/ build/web/js/substandard_lisp.js build/web/js/biginteger.js

node_core: build/node/ build/node/substandard_lisp.js build/node/biginteger.js

dist: all
	echo substandard_lisp-$(VERSION) > .fname
	-rm -rf `cat .fname`
	mkdir `cat .fname`
	cp -r Makefile README VERSION build src deps `cat .fname`
	tar -cz --exclude-vcs -f `cat .fname`.tar.gz `cat .fname`
	-rm -rf `cat .fname` .fname

node_run: node_core
	node build/node/substandard_lisp.js

web_terminal: web_core build/web/css/ build/web/js/substandard_terminal.js \
		build/web/js/jquery-1.5.min.js build/web/js/jquery.mousewheel.min.js \
		build/web/js/jquery.terminal-0.3.4.js build/web/css/jquery.terminal.css \
		build/web/terminal.htm build/web/shell.htm

test: node_test

web_test: web_core build/web/css/ build/web/js/test.js build/web/js/qunit.js \
	build/web/test.htm build/web/css/qunit.css

node_test:

web_reduce: web_core build/web/reduce.htm build/web/js/substandard_reduce.js

web_examples: web_core $(foreach file,$(EXAMPLES),build/web/$(basename $(notdir $(file))).htm)

build/tmp/builtins.yaml: build/tmp/ $(BUILTINS)
	python src/util/make_yaml.py src/interpreter/builtins > $@

build/tmp/sl_pre.js: build/tmp/builtins.yaml src/interpreter/core.coffee
	(cat src/interpreter/core.coffee; python src/util/make_builtins.py < build/tmp/builtins.yaml) | coffee -psbc > $@

build/web/js/substandard_lisp.js: build/web/js/ build/tmp/sl_pre.js
	cpp -P -C build/tmp/sl_pre.js > $@

build/node/substandard_lisp.js: build/tmp/sl_pre.js src/node/node_sl.coffee
	cpp -P -C -D node build/tmp/sl_pre.js > $@
	coffee -pbc src/node/node_sl.coffee >> $@

build/web/js/test.js: src/web/test/js/test.js build/tmp/builtins.yaml
	-rm -f $@
	cp src/web/test/js/test.js $@
	python src/util/make_tests.py < build/tmp/builtins.yaml >> $@

build/web/js/substandard_reduce.js: src/reduce/substandard_reduce.js $(REDUCE_FILES)
	-rm -f $@
	cp src/reduce/substandard_reduce.js $@
	python src/util/make_files.py src/reduce/reduce_files/ >> $@

build/web/js/%.js: src/web/*/js/%.js
	cp $< $@

build/web/js/%.js: deps/%.js
	cp $< $@

build/node/%.js: deps/%.js
	cp $< $@

build/web/css/%.css: src/web/*/css/%.css
	cp $< $@

build/web/%.htm: src/web/examples/%.lsp
	python src/util/lsp2htm.py $< > $@

build/web/%.htm: src/web/*/%.htm
	cp $< $@

build/%/:
	mkdir -p $@
